$(function(){
    $("#pjdh_add").click(function(){
        appendPjdh();
    });
    $("#pjdh_del").click(function(){
        removePjdh();
    });
    $('#pjdh_pj_grid').edatagrid({
        showFooter:true
    });

});
//增添票据兑换
function appendPjdh() {
    $('#pjdh_pj_grid').edatagrid('addRow');
        //获取每个单元格
        var row =  $("#pjdh_pj_grid").edatagrid('getSelected');
        var rowIndex =  $("#pjdh_pj_grid").edatagrid('getRowIndex',row);
        var editors = $("#pjdh_pj_grid").datagrid('getEditors', rowIndex);
        if (editors.length == 0)
            return;

        editors[0].target.combobox({
            width:123,
            onSelect:function(rowData){
               // $(editors[8].target).numberbox("setValue",rowData.id);
                row['nbgs.id']=rowData.id;
                $(editors[1].target).combobox('clear');
                $(editors[1].target).combobox("reload",$.webPath() + '/organ/khh/khhmcList?nbgsId=' + rowData.id);
            }
        });
    editors[1].target.combobox({
        width:88,
        onSelect:function(rowData){
            row['yfcpyh.id']=rowData.id;
//            $(editors[9].target).numberbox("setValue",rowData.id);
        }
    });
    editors[2].target.combobox({
        width:88,
        onSelect:function(rowData){
            row['pjlx']=rowData.id;
//            $(editors[10].target).numberbox("setValue",rowData.id);
        }
    });
    countSumPjdh();
}
//删除运输工具
function removePjdh() {
    var gjRow = $('#pjdh_pj_grid').datagrid('getSelected');
    if(gjRow){
        $('#pjdh_pj_grid').edatagrid('destroyRow');
    } else {
        $.messager.alert('提示','请选择要删除的票据!');
    }

}
//取消运输工具
function cancelPj() {
    $('#pjdh_pj_grid').edatagrid('cancelRow');
}

//计算计划总量
function countSumPjdh() {
    var sumje = 0;
    var data = $("#pjdh_pj_grid").edatagrid("getData");
    for (var i = 0; i < data.rows.length; i++) {
        var je=parseFloat(data.rows[i]['dqje']);
        je = isNaN(je) ? 0 : je;
        sumje += je;
    }
    addFooter(parseInt(sumje));
}

function addFooter(sumje) {
    var footer = null;
    footer = [{"bzj": "合计", "dqje": formatMoney(sumje)}];
    if (footer != null)
        $("#pjdh_pj_grid").edatagrid('reloadFooter',footer);
}
